'use strict';

const Controller = require('egg').Controller;

// TODO 因为安不上kafka于是就决定先不上MQ了

class MessageController extends Controller {
  async index() {
    console.log(this.ctx.request.body);
    const start = Date.now();
    this.ctx.body = await this.ctx.service.message.index(this.ctx.query.page);
    this.ctx.logger.info(`查询耗时:${Date.now() - start}ms`);
  }
  async sendMessage() {
    try {
      this.ctx.validate({
        nick: { type: 'string' },
        message: { type: 'string' },
      });
    } catch (e) {
      this.ctx.throw(422);
    }
    const start = Date.now();
    const insertStatus = await this.ctx.service.message.sendMessage(this.ctx.request.body);
    this.ctx.logger.info(`插入耗时:${Date.now() - start}ms`);
    if (insertStatus.code === 200) {
      console.log(insertStatus);
      this.ctx.body = insertStatus;
    } else {
      this.ctx.throw(500);
    }
  }
}

module.exports = MessageController;
